<?php

autoLoader();

$mid 	= ( ( true == isset( $_GET['mid'] ) && true == valStr( $_GET['mid'] ) ) ? $_GET['mid'] : NULL );
$smid 	= ( ( true == isset( $_GET['smid'] ) && true == valStr( $_GET['smid'] ) ) ? $_GET['smid'] : NULL );
$emp_id = ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) && true == valStr( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : NULL;

if( false == valStr( $mid ) ) $mid = ( ( true == isset( $_POST['mid'] ) && true == valStr( $_POST['mid'] ) ) ? $_POST['mid'] : NULL );
if( false == valStr( $smid ) ) $smid = ( ( true == isset( $_POST['smid'] ) && true == valStr( $_POST['smid'] ) ) ? $_POST['smid'] : NULL );

	$objPatient 	= new BasePatients();
	$objPatients 	= new Patients();
	$objCasePaper 	= new CasePapers();
	
	$arrobjWardCatagories = new WardCatagories();
	$arrobjWardCatagories = $arrobjWardCatagories->fetchAllWardCatagories();

	$intPatientId 			= '';
	$intCasePaperId			= '';
	$strName 				= '';
	
	if( true == isset( $_GET['patient_id'] ) && true == valStr( $_GET['patient_id'] ) ) {
		$intPatientId = $_GET['patient_id'];
		
		$objPatient 	= $objPatients->fetchPatientsById( $intPatientId );
		$objCasePaper 	= $objCasePaper->fetchCasePapersById( $_GET['case_paper_id'] );
		
		if( true == valObj( $objPatient, 'BasePatients' ) ) {
			$strName = $objPatient->getFirstName() . ' ' . $objPatient->getLastName();
			
			$strSql = 'SELECT * FROM medical_case_sheets WHERE patient_id = ' . $objPatient->getId() . ';';
			// need to use this condition here in fature. "discharge_date IS NULL";
			
			$objMedicalCaseSheet = new MedicalCaseSheets();
			$objMedicalCaseSheet = $objMedicalCaseSheet->fetchSql( $strSql );
			
			$objWard 			= new Wards();
			$objWardType 		= new WardTypes();
			$objWardCatagory 	= new WardCatagories();
			
			$objPatientWard = $objPatientWardType = $objPatientWardCatagory = NULL;
			$strRoomNumber = NULL;
			
			$objMedicalCaseSheet = current( $objMedicalCaseSheet );
			
			if( true == valObj( $objMedicalCaseSheet, 'BaseMedicalCaseSheets' ) ) {
				$objPatientWard = $objWard->fetchWardsById( $objMedicalCaseSheet->getWardId() );
				
				if( true == valObj( $objPatientWard, 'BaseWards' ) ) {
					$strRoomNumber = $objPatientWard->getRoomNumber();
					$objPatientWardType = $objWardType->fetchWardTypesById( $objPatientWard->getWardTypeId() );
				}
				
				if( true == valObj( $objPatientWardType, 'BaseWardTypes' ) ) {
					$objPatientWardCatagory = $objWardCatagory->fetchWardCatagoriesById( $objPatientWardType->getWardCatagoryId() );
				}
			}
		}
		
		if( true == valObj( $objCasePaper, 'BaseCasePapers' ) ) {
			$intCasePaperId = $objCasePaper->getId();
		}
	}
?>
<div style="margin-top:5px; margin-bottom: 10px;">
	<?php
		if( true == valObj( $objPatient, 'BasePatients' ) ) {
			echo '<input type="button" value="Add New Note" name="add_yellow_pages" onClick="window.location.href=\'modules.php?file=ipd_yellow_page&action=add&patient_id=' . $objPatient->getId() . '&mid=' . $mid . '&smid=' . $smid . '\'" class="btn btn-primary" />&nbsp;&nbsp;';
			echo '<input type="button" value="View Yellow Pages" name="yellow_pages" onClick="window.location.href=\'modules.php?file=ipd_yellow_page&patient_id=' . $objPatient->getId() . '&mid=' . $mid . '&smid=' . $smid . '\'" class="btn btn-primary" />&nbsp;&nbsp;';
			echo '<a href="modules.php?file=print_case_sheet&type=print&action=print&patient_id=' . $intPatientId . '&case_paper_id=' . $intCasePaperId . '&mid=' . $mid . '&smid=' . $smid . '"><input type="button" value="Print Case Sheet" name="case_sheet" class="btn btn-primary" /></a>';
		}
	?>
</div>
<div style="border-style: solid; display: block;">
	<div style="padding: 2px;display: none;margin:5px 5px 0px 5px;" class="alert alert-success" id="admission_success_display">
		<img alt="admission_insert_success Found with info.." src="images/ok.png" width="20" height="20">
		<label id='admission_insert_success'></label>
	</div>
	
	<div style="padding: 2px;display: none;margin:5px 5px 0px 5px;" class="alert alert-danger" id="admission_failed_display">
		<img alt="Patient Found with info.." src="images/close.png" width="20" height="20">
		<label id='admission_insert_failed'></label>
	</div>
	
	<div style="clear: both;"></div>
	
	<?php
		if( true == valObj( $objPatientWard, 'BaseWards' ) ) {
	?>
		<div class="alert alert-success">
			<?php echo '<b>' . $strName . '</b> addmitted at Room Number::' . $strRoomNumber . '.'; ?> 
		</div>
	<?php
		}
	?>
		<table width="100%" cellspacing="0" cellpadding="3" style="margin-left: px;" class="table table-striped table-bordered table-hover dataTable no-footer">
			<tr valign="top">
				<td class="mandatory">
					Select Ward Catagory:
				</td>
				<td>
					<select name="ward_catagory_id" id="ward_catagory_id">
						<option id="ward_catagory_id_null" value="">--Select Ward Catagory--</option>
					<?php
						$objSelectedWardCatagory = NULL;
						
						if( true == valArr( $arrobjWardCatagories ) ) {
							foreach( $arrobjWardCatagories as $objWardCatagory ) {
								$strSelected = ( ( true == valObj( $objPatientWardCatagory, 'BaseWardCatagories' ) && $objPatientWardCatagory->getId() == $objWardCatagory->getId() ) ? 'selected=selected' : PatientType::getPatientByWardCatagory( $objPatient, $objWardCatagory ) );
								if( true == valStr( $strSelected ) ) {
									$objSelectedWardCatagory = $objWardCatagory;
								}
								echo '<option ' . $strSelected . ' id="' . $objWardCatagory->getId() . '" value="' . $objWardCatagory->getId() . '">' . $objWardCatagory->getName() . '</option>';
							}
						}
					?>
					</select>
				</td>
				<td>&nbsp;</td>
			</tr>
			<tr>
				<td>Select Ward Type:-</td>
				<td>
					<?php
						if( true == valObj( $objSelectedWardCatagory, 'BaseWardCatagories' ) ) {
							$strSql = 'SELECT * FROM ward_types WHERE ward_catagory_id = ' . $objSelectedWardCatagory->getId();
							
							$arrobjWardTypes = new WardTypes();
							$arrobjWardTypes = $arrobjWardTypes->fetchSql( $strSql );
						}
					?>
					<select name="ward_type_id" id="ward_type_id">
						<option id="ward_type_id_null" value="">--Select Ward Type--</option>
						<?php
							if( true == valArr( $arrobjWardTypes ) ) {
								foreach( $arrobjWardTypes as $objWardType ) {
									$strSelected = ( true == valObj( $objPatientWardType, 'BaseWardTypes' ) && $objPatientWardType->getId() == $objWardType->getId() ) ? 'selected=selected' : NULL;
									echo '<option ' . $strSelected . ' value="' . $objWardType->getId() . '" id="' . $objWardType->getId() . '">' . $objWardType->getName() . '</option>';
								}
							}
						?>
					</select>
				</td>
			</tr>
			<tr>
				<td>Select Ward:-</td>
				<td>
					<?php
						$arrobjWardTypes = rekeyObjects( 'Id', $arrobjWardTypes );
						
						$arrobjWards = new Wards();
						
						if( true == valArr( $arrobjWardTypes ) ) {
							$strSql 		= 'SELECT * FROM wards WHERE ward_type_id IN( ' . implode( ',', array_keys( $arrobjWardTypes ) ) . ' );';
							$arrobjWards 	= $arrobjWards->fetchSql( $strSql );
						} else {
							$arrobjWards 	= $arrobjWards->fetchAllWards();
						}
					?>
					<select name="ward_id" id="ward_id">
						<option id="ward_id_null" value="">--Select Ward--</option>
						<?php
							if( true == valArr( $arrobjWards ) ) {
								foreach( $arrobjWards as $objWard ) {
									$strSelected = ( true == valObj( $objPatientWard, 'BaseWards' ) && $objPatientWard->getId() == $objWard->getId() ) ? 'selected=selected' : NULL;
									echo '<option ' . $strSelected . ' value="' . $objWard->getId() . '" id="' . $objWard->getId() . '">' . $objWard->getRoomNumber() . '</option>';
								}
							} elseif( true == valObj( $objPatientWard, 'BaseWards' ) ) {
								echo '<option selected=selected value=' . $objPatientWard->getId() . '>' . $objPatientWard->getRoomNumber() . '</option>';
							}
						?>
					</select>
				</td>
			</tr>
		</table>
	<div style="text-align: center;">
		<?php
			if( true == isset( $_GET['admission_id'] ) && true == valStr( $_GET['admission_id'] ) || true == valObj( $objPatientWard, 'BaseWards' ) ) { ?>
				<input type="hidden" name="operation" id="operation" value="update" />
				<input type="submit" name="Submit" id="submit" value="UPDATE" class="btn btn-primary">
				<a href="modules.php?file=patient_admission&mid=<?php echo $mid;?>&smid=<?php echo $smid;?>"><input type="button" name="cancel" id="cancel" class="btn btn-primary" value="Cancle"></a>
		<?php 	} else { ?>
				<input type="hidden" name="operation" id="operation" value="insert" />
				<input type="button" name="Submit" id="submit" value="Addmit Patient" class="btn btn-primary" onclick="handleAdmitPatient();" />
				<input type="button" name="cancel" id="cancel" value="Cancel" class="btn btn-primary">
		<?php } ?>
		<a id="print_opd_form" style="display: none;" href="modules.php?file=patient_admission&type=print&mid=<?php echo $mid;?>&smid=<?php echo $smid;?>"><input type="button" name="print" id="print" class="btn btn-primary" value="PRINT"></a>
	</div>
</div>

<script>
	$('#ward_catagory_id').change( function() {
		
		var intWardCatagoryId =	$(this).val();

		var getParams={ ward_type_id:intWardCatagoryId };

		$.getJSON( 'modules.php?file=fetch_list_items&type=fetch&action=ward_types&ward_catagory_id=' + intWardCatagoryId, getParams,
		function( data ) {
			strWardTypeListData = "<option id='' value=''>--Select Ward Type--</option>";
			
			if( data != 'not found::' ) {
				for( var i = 0; i < data.length; i++ ) {
					strWardTypeListData += '<option id="' + data[i]['id'] + '" value="' + data[i]['id'] + '">' + data[i]['name'] + '</option>';
				}

				$('#ward_type_id').html( strWardTypeListData );
			}
		});
	});

	$('#ward_type_id').change( function() {
		
		var intWardTypeId =	$(this).val();

		var getParams={ ward_type_id:intWardTypeId };

		$.getJSON( 'modules.php?file=fetch_list_items&type=fetch&action=wards&ward_type_id=' + intWardTypeId, getParams,
		function( data ) {
			strWardListData = "<option id='' value=''>--Select Ward--</option>";

			if( data == "not found::" ) {
				$('#ward_id').html( strWardListData );
			} else {
				for( var i = 0; i < data.length; i++ ) {
					strWardListData += '<option id="' + data[i]['id'] + '" value="' + data[i]['id'] + '">' + data[i]['room_number'] + '</option>';
				}

				$('#ward_id').html( strWardListData );
			}
		});

	});
</script>